package base

import (
	"fmt"
	"strings"
)

type TableColumn struct {
	TableName  string
	ColumnName string
}

func NewTableColumn(tableName string, columnName string) *TableColumn {
	return &TableColumn{
		TableName:  tableName,
		ColumnName: columnName,
	}
}

func (tc *TableColumn) Less(other *TableColumn) bool {
	if tc.TableName != other.TableName {
		return tc.TableName < other.TableName
	} else {
		return tc.ColumnName < other.ColumnName
	}
}

func (tc *TableColumn) String() string {
	return fmt.Sprintf("%s.%s", tc.TableName, tc.ColumnName)
}

func (tc *TableColumn) Equal(other *TableColumn) bool {
	return tc.String() == other.String()
}

type FunctionColumn struct {
	FunctionName string
	Args         []*TableColumn
}

func NewFunctionColumn(functionName string, args ...*TableColumn) *FunctionColumn {
	return &FunctionColumn{
		FunctionName: functionName,
		Args:         args,
	}
}

func (fc *FunctionColumn) String() string {
	var b strings.Builder
	for i := range fc.Args {
		if i != 0 && i != len(fc.Args)-1 {
			b.WriteString(",")
		}
		b.WriteString(fc.Args[i].String())
	}

	return fmt.Sprintf("%s(%s)", fc.FunctionName, b.String())
}

func (fc *FunctionColumn) Equal(other *FunctionColumn) bool {
	return fc.String() == other.String()
}
